package com.example.byd

import android.annotation.SuppressLint
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NavUtils
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager
import java.util.*


var switch_merageisopen=true
var switch_radar=true





class SettingsActivity : AppCompatActivity()  {






    @SuppressLint("RestrictedApi")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.settings_activity)
        if (savedInstanceState == null) {
            supportFragmentManager
                    .beginTransaction()
                    .replace(R.id.settings, SettingsFragment())
                    .commit()
        }
        supportActionBar?.setDisplayHomeAsUpEnabled(true)


    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId
//        Log.i("bydautosong", "get id is ${item.itemId}")

        if (id==android.R.id.home){
//            val intent = Intent(this, MainActivity::class.java)
//            startActivity(intent)
            finish()
            Log.i("bydautosong", "get  1111 id is ${item.itemId}")
        }
//
        return super.onOptionsItemSelected(item)
    }



    class SettingsFragment : PreferenceFragmentCompat() {
//设置发生改变时重新设置
        val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, changedKey ->
             val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
            if (changedKey == "switch_merage") {

                val value = sharedPreferences.getBoolean(changedKey,true)
                switch_merageisopen=value
                Log.i("bydautosong", "当前辅助并线的状态是"+value)

            }else if(changedKey == "switch_radar"){
                val value = sharedPreferences.getBoolean(changedKey,true)
                switch_radar=value
                val bv:String
                when(value){
                    true -> bv="开启"
                    false -> bv="关闭"
                }
                Log.i("bydsongtts", "雷达播报:"+bv)
            }else if(changedKey == "voice-engine" ){
                val value = sharedPreferences.getString(changedKey,"com.xiaomi.mibrain.speech")
                var enginename = "未设置"
                if(value=="com.xiaomi.mibrain.speech"){
                    enginename="小米小爱"
                }
                if(value=="com.iflytek.vflynote"){
                    enginename="讯飞语音"
                }


//                GetLogCatService.

                Log.i("bydsongtts","语音引擎设置为：${enginename}")


            }
            else if(changedKey == "switch_open_acc"){
                val value = sharedPreferences.getBoolean(changedKey,true)
                switch_radar=value
                val bv:String
                when(value){
                    true -> bv="开启"
                    false -> bv="关闭"
                }
                Log.i("bydsongtts", "a c c播报:"+bv)
            }


            else if(changedKey == "speed_limit"){
                val value = sharedPreferences.getString(changedKey,"120")
                Log.i("bydsongtts", "测速告警调整为:"+value)
            }


//            val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
//            val value = sharedPreferences.getString(changedKey, "")
//            println("改变的key是:"+changedKey+",值是:"+value)
        }



        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey)

//
//            val signaturePreference: EditTextPreference? = findPreference("signature")
//            val leidakey: EditTextPreference? = findPreference("leidakey")
        }

        override fun onResume() {
            super.onResume()
            preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(listener)
        }

        override fun onPause() {
            super.onPause()
            preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(listener)
        }


    }

}


